/*to do list
inpadmenu operation eg. back to menu...save file...
*/
#include <stdlib.h>
#include <pspctrl.h>
#include "Pad.h"
#include "../HandleKeyBoard/HandleKeyBoard.h"
#include "../Core/Key.h"
#include "../Core/MyGraphic.h"
#include "../Core/directory.h"
#include "../Core/fileio.h"
#include "../Interface/KeyBoard.h"
#include "../Interface/Menu.h"
#include "../States/State.h"

int m_inpad_menu_active = 0;

unsigned char m_inpad_menu[INPAD_MENU_COUNT][INPAD_MENU_ITEM_LEN] = {
	"SAVE FILE\0",
	"RELOAD FILE\0",
	"EXIT TO MENU\0",
	};

int m_inpad_menu_state_dir[INPAD_MENU_COUNT] = {
	INPAD_MENU_SAVE, 
	INPAD_MENU_RELOAD,
	INPAD_MENU_BACK_TO_MENU
	};

int m_inpad_menu_selected = 0;


int InitPad(unsigned char* buffer, int txtlen, int cursorpos, int color, int fontsize)
{
	m_padbuffer = buffer;
	m_padtxtlen = txtlen;
	m_padbufferpos = cursorpos;
	m_padfontcolor = color;
	m_padfontsize = fontsize;
}

int DrawPad(int x, int y)
{
	DrawStringWithCursor(x, y, m_padbuffer, m_padtxtlen, m_padfontcolor, m_padfontsize, m_padbufferpos);
}

int UpdateKeyPad(int isSave)
{
		if(IsPressed(PSP_CTRL_TRIANGLE))
		{
			MoveKey(KEY_MOVE_UP, RIGHT_SIDE);
		}
		if(IsPressed(PSP_CTRL_CIRCLE))
		{
			MoveKey(KEY_MOVE_RIGHT, RIGHT_SIDE);
		}
		if(IsPressed(PSP_CTRL_SQUARE))
		{
			MoveKey(KEY_MOVE_LEFT, RIGHT_SIDE);
		}
		if(IsPressed(PSP_CTRL_CROSS))
		{		
			MoveKey(KEY_MOVE_DOWN, RIGHT_SIDE);
		}
		if(IsPressed(PSP_CTRL_UP))
		{
			MoveKey(KEY_MOVE_UP, LEFT_SIDE);
		}
		if(IsPressed(PSP_CTRL_DOWN))
		{
			MoveKey(KEY_MOVE_DOWN, LEFT_SIDE);
		}
		if(IsPressed(PSP_CTRL_LEFT) && !IsHold(PSP_CTRL_RTRIGGER))
		{
			MoveKey(KEY_MOVE_LEFT, LEFT_SIDE);
		}
		if(IsPressed(PSP_CTRL_RIGHT) && !IsHold(PSP_CTRL_RTRIGGER))
		{
			MoveKey(KEY_MOVE_RIGHT, LEFT_SIDE);
		}
//		if(IsPressed(PSP_CTRL_SELECT))
//		{
//		}
//		if(IsPressed(PSP_CTRL_START))
//		{
//		}
		if(IsPressed(PSP_CTRL_RTRIGGER))
		{
			if(isSave && KeyBoardPressed(RIGHT_SIDE) == KEY_CTRL_ENTER)
			{
				SetState(STATE_SAVE);
				return;
			}
			HandleKeyBoardPressed(KeyBoardPressed(RIGHT_SIDE), m_padbuffer, &m_padbufferpos, &m_padtxtlen, BUFFER_SIZE);
		}
		if(IsPressed(PSP_CTRL_LTRIGGER))
		{
			HandleKeyBoardPressed(KeyBoardPressed(LEFT_SIDE), m_padbuffer, &m_padbufferpos, &m_padtxtlen, BUFFER_SIZE);
		}
		if(IsHold(PSP_CTRL_RTRIGGER) && IsPressed(PSP_CTRL_LEFT))
		{
			MoveCursor(m_padbuffer, &m_padbufferpos, &m_padtxtlen, BUFFER_SIZE, CURSORLEFT);
		}
		if(IsHold(PSP_CTRL_RTRIGGER) && IsPressed(PSP_CTRL_RIGHT))
		{
			MoveCursor(m_padbuffer, &m_padbufferpos, &m_padtxtlen, BUFFER_SIZE, CURSORRIGHT);
		}
		
		if(IsPressed(PSP_CTRL_SELECT))
		{
			m_inpad_menu_active = !m_inpad_menu_active;
		}
		
		return 0;
}

int DrawInPadMenu()
{
	int i;
	MyFillRect(INPAD_MENU_BOX_X, INPAD_MENU_BOX_Y, INPAD_MENU_BOX_WIDTH, INPAD_MENU_BOX_HEIGHT, INPAD_MENU_BOX_COLOR);
	for(i = 0; i< INPAD_MENU_COUNT; i++)
	{
		DrawString(INPAD_MENU_X, INPAD_MENU_Y + i * (INPAD_MENU_ITEM_SPACE + FONT_HEIGHT), m_inpad_menu[i], INPAD_MENU_ITEM_LEN, (m_inpad_menu_selected==i)?INPAD_MENU_ITEM_SELECTED_COLOR:INPAD_MENU_ITEM_COLOR, 1);
	}
	return 0;
}

int UpdateInPadMenu()
{
	if(IsPressed(PSP_CTRL_UP))
	{
			m_inpad_menu_selected--;
			if(m_inpad_menu_selected<0)
				m_inpad_menu_selected = 0;
	}
	if(IsPressed(PSP_CTRL_DOWN))
	{
			m_inpad_menu_selected++;
			if(m_inpad_menu_selected>=INPAD_MENU_COUNT)
				m_inpad_menu_selected = INPAD_MENU_COUNT-1;
	}
	if(IsPressed(PSP_CTRL_CIRCLE))
	{
		switch(m_inpad_menu_state_dir[m_inpad_menu_selected])
		{
			case INPAD_MENU_SAVE:
				SetState(STATE_SAVE_INIT);
				break;
			case INPAD_MENU_RELOAD:
				memset(txtbuffer, 0, BUFFER_SIZE);
				txtlen = ReadFile(curfilepath, dirlist[dir_selected].dirname, txtbuffer);			
				m_padbufferpos = 0;
				break;
			case INPAD_MENU_BACK_TO_MENU:
				SetState(STATE_MENU);
				break;
		}
		m_inpad_menu_selected = 0;
		m_inpad_menu_active = 0;
	}
	
	return 0;
}

int DeletePad()
{
	m_padbuffer = NULL;
	return 0;
}

int GetPadLen()
{
	return m_padtxtlen;
}